<template>
    <div class="login-wrap">
    <div class="login-root">
      <div class="login-main">
        <img class="login-one-ball"
          src="../img/login-one.png" />
        <img class="login-two-ball"
          src="../img/login-two.png" />
        <div class="login-container">
          <div class="login-side">
            <div class="login-bg-title">
              <img src="../img/login-logo.png" style="width:60px;"/>

              <h1 style="margin: 20px auto;color:#fff;">
                权 限 管 理 系 统
              </h1> 
            </div>
          </div>
          <div class="login-ID">
            <div style="font-size: 22px; margin-bottom: 15px; margin-top: 5px">
              账号登录
            </div>
           <div style="height: 250px">
                <a-form id="formLogin" ref="formLogin" :form="form" @submit="handleSubmit">
                        <a-form-item>
                            <a-input size="large" type="text" placeholder="账号" v-decorator="['accountNo',{ rules: [{ required: true, message: '请输入账号' }], validateTrigger: 'change' },]" >
                                <template #prefix>
                                    <a-icon type="user" />
                                </template>
                            </a-input>
                        </a-form-item>
                        <a-form-item>
                            
                            <a-input-password size="large" placeholder="密码" v-decorator="['empPwd',{ rules: [{ required: true, message: '请输入密码' }], validateTrigger: 'blur' },]">
                                <template #prefix>
                                    <a-icon type="lock" />
                                </template>
                            </a-input-password>
                        </a-form-item>

                        <a-form-item v-if="defaultSettings.showIdentifyCode">
                            <a-row>
                                <a-col span="16">
                                    <a-input size="large" type="text" placeholder="验证码" v-decorator="['validateCode',{ rules: [{ required: true, message: '请输入验证码' }], validateTrigger: 'change' },]" >
                                    </a-input>
                                </a-col>
                                <a-col span="8">
                                    <div @click="refreshCode">
                                        <SIdentify :identifyCode="identifyCode" :contentHeight="45"></SIdentify>
                                    </div>
                                    
                                </a-col>
                            </a-row>
                        </a-form-item>

                        <a-form-item style="margin-top: 24px">
                            <a-button
                                block
                                size="large"
                                type="primary"
                                htmlType="submit"
                                :loading="state.loginBtn"
                                :disabled="state.loginBtn"
                                >登陆</a-button
                            >
                        </a-form-item>
                    </a-form>
                
            </div>
           
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import md5 from 'md5'
import { login,generateKey } from '@/api/login'
import { ACCESS_TOKEN } from '@/store/mutation-types'
import JSEncrypt from 'jsencrypt'
import defaultSettings from '@/config/defaultSettings'
import SIdentify from '@/components/lt/SIdentify'

export default {
    components: {
        SIdentify
    },
    data() {
        return {
            loginBtn: false,
            // login type: 0 email, 1 username, 2 telephone
            loginType: 0,
            isLoginError: false,
            requiredTwoStepCaptcha: false,
            stepCaptchaVisible: false,
            form: this.$form.createForm(this),
            state: {
                time: 60,
                loginBtn: false,
                loginType: 0,
                smsSendBtn: false,
            },
            defaultSettings,
            identifyCodes: "1234567890",
            identifyCode:''
        }
    },
    created() {
        this.refreshCode()
    },
    methods: {
        handleSubmit(e) {
            e.preventDefault()
            const {form: { validateFields },state,Login,} = this

            this.form.validateFields((err, values) => {
                if(this.defaultSettings.showIdentifyCode){
                    //判断验证码是否正确
                    if(values.validateCode!=this.identifyCode){
                        this.$message.warning("验证码错误")
                        state.loginBtn = false
                        return false
                    }
                }

                if (!err) {
                    state.loginBtn = true
                    let loginParams = Object.assign({}, values)

                    //先获取密钥
                    generateKey().then((res)=>{
                        if(!res.success || !res.message){
                            this.$message.warning("密钥获取失败")
                            state.loginBtn = false
                        }
                        let encryptor = new JSEncrypt()
                        encryptor.setPublicKey(res.message)
                        loginParams.empPwd = encryptor.encrypt(loginParams.empPwd)
                        login(loginParams)
                            .then((res) => this.loginSuccess(res))
                            .catch((err) => this.requestFailed(err))
                            .finally(() => {state.loginBtn = false})
                    }).finally(() => {state.loginBtn = false})

                    
                } else {
                    state.loginBtn = false
                }
            })
        },

        loginSuccess(res) {
            if (res.success) {
                sessionStorage.setItem(ACCESS_TOKEN, res.result.token)
                this.refreshCode()
                this.$router.push({ path: '/' })
            } else {
                this.$notification.error({ message: '系统提示', description: res.message })
            }

            this.isLoginError = false
        },
        requestFailed(err) {
            this.isLoginError = true
            this.$notification['error']({
                message: '错误',
                description: ((err.response || {}).data || {}).message || '请求出现错误，请稍后再试',
                duration: 4,
            })
        },
        randomNum(min, max) {
            return Math.floor(Math.random() * (max - min) + min);
        },
        refreshCode() {
            this.identifyCode = "";
            this.makeCode(this.identifyCodes, 4);
        },
        makeCode(o, l) {
            for (let i = 0; i <l; i++) {
                this.identifyCode += this.identifyCodes[
                    this.randomNum(0, this.identifyCodes.length)
                ];
            }
            // console.log(this.identifyCode);
        }
    },
}
</script>

<style scoped>
    .login-captach {
        display: inline-block;
        vertical-align: bottom;
        width: 108px;
        height: 40px;
        color: #0977AF;
        margin-left: 8px;
        border-radius: 4px;
        border: 1px solid hsla(0, 0%, 60%, 0.46);
        transition: border 0.2s;
        box-sizing: border-box;
        background: #fff;
        overflow: hidden;
        cursor: pointer;
        }

        .login-one-ball {
        opacity: 0.4;
        position: absolute;
        max-width: 568px;
        left: -400px;
        bottom: 0px;
        }

        .login-two-ball {
        opacity: 0.4;
        position: absolute;
        max-width: 320px;
        right: -200px;
        top: -60px;
        }

        .login-wrap {
            position: fixed;
            top: 0;
            left: 0;
            bottom: 0;
            right: 0;
            overflow: auto;
            min-width: 600px;
            z-index: 9;
            background-image: url(../img/login-bg.png);
            background-repeat: no-repeat;
            background-size: cover;
            min-height: 100vh;
        }

        .login-wrap :deep(.layui-input-block) {
            margin-left: 0 !important;
        }

        .login-root {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            display: flex;
            justify-content: center;
            width: 100%;
            min-width: 320px;
            background-color: initial;
        }

        .login-main {
            position: relative;
            display: block;
        }

        .logo-container {
            max-width: calc(100vw - 28px);
            margin-bottom: 40px;
            text-align: center;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .logo-container .logo {
            display: inline-block;
            height: 30px;
            width: 143px;
            background: no-repeat 50%;
            background-size: contain;
            cursor: pointer;
        }

        .login-container {
            position: relative;
            overflow: hidden;
            width: 940px;
            height: 540px;
            max-width: calc(100vw - 28px);
            border-radius: 4px;
            background: hsla(0, 0%, 100%, 0.5);
            backdrop-filter: blur(30px);
            display: flex;
            box-shadow: 6px 6px 12px 4px rgba(0, 0, 0, 0.1);
        }

        .login-side {
            padding: 40px 20px 20px;
            background-color:#0977AF;
            flex: 1;
            height: 100%;
        }

        .login-bg-title {
            flex: 1;
            height: 84%;
            color: #fff;
            text-align: center;
            background-image: url('../img/login-bg.svg');
            background-repeat: no-repeat;
            background-position: bottom;
            background-size: contain;
            text-align: center;
            min-width: 200px;
        }

        .login-ID {
            padding: 20px 30px;
            min-width: 420px;
        }

        .login-container .layui-tab-head {
            background: transparent;
        }

        .login-container .layui-input-wrapper {
            margin-top: 10px;
            margin-bottom: 10px;
        }

        .login-container .layui-input-wrapper {
            margin-top: 12px;
            margin-bottom: 12px;
        }

        .login-container .assist {
            margin-top: 5px;
            margin-bottom: 5px;
            letter-spacing: 2px;
        }

        .login-container .layui-btn {
            margin: 10px 0px 10px 0px;
            letter-spacing: 2px;
            height: 40px;
        }

        .login-container .layui-line-horizontal {
            letter-spacing: 2px;
            margin-bottom: 34px;
            margin-top: 24px;
        }

        .other-ways {
            display: flex;
            justify-content: space-between;
            margin: 0;
            padding: 0;
            list-style: none;
            font-size: 14px;
            font-weight: 400;
        }

        .other-ways li {
            width: 100%;
        }

        .line-container {
            justify-content: center;
            align-items: center;
            text-align: center;
            cursor: pointer;
        }

        .line-container .icon {
            height: 28px;
            width: 28px;
            margin-right: 0;
            vertical-align: middle;
            border-radius: 50%;
            background: #fff;
            box-shadow: 0 1px 2px 0 rgb(9 30 66 / 4%), 0 1px 4px 0 rgb(9 30 66 / 10%),
            0 0 1px 0 rgb(9 30 66 / 10%);
        }

        .line-container .text {
            display: block;
            margin: 12px 0 0;
            font-size: 12px;
            color: #8592a6;
        }

    ::v-deep .ant-input{
        background-color: #F8F8F8 !important;
        color: #000000 !important;
        height: 50px !important;
        border:none;
        border-radius: 20px; 
    }
    ::v-deep .ant-btn {
        color: #ffffff !important;
        background-color: #0977AF !important;
        border-color:#0977AF !important;
        height: 50px !important;
        border-radius: 20px; 
    }
    ::v-deep .ant-form-item{
        margin-top:20px !important;
    }
</style>
